//#include "serialportwidget.h"
#include "cputemp.h"
#include <QLabel>
#include <QLineEdit>
#include <QComboBox>
#include <QPushButton>
#include <QGridLayout>
#include <QString>
#include <QTimer>
#include <QList>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <qdebug.h>
#include <qfile.h>
#include <QFile>
#include <sys/ioctl.h>
#include <unistd.h>
#include <QString>
#include <QNetworkInterface>
#include <QList>
#include <QFrame>

CpuTemp::CpuTemp(QWidget *parent) : JDialog(parent)
{
    QGridLayout *pMainLayout = new QGridLayout();

    pMainLayout->setMargin(SUBSPACE);
    pMainLayout->setSpacing(SUBSPACE);

    m_pCpufreqLab[0] = new QLCDNumber();
    m_pCpufreqLab[1] = new QLCDNumber();
    m_pCpufreqLab[2] = new QLCDNumber();
    m_pCpufreqLab[3] = new QLCDNumber();

    m_pTempLab = new QLCDNumber();
    m_pTimeLab = new QLCDNumber();

    for(int i = 0; i < 4; i++){
        m_pCpufreqLab[i]->setFixedWidth(300);
        m_pCpufreqLab[i]->setFixedHeight(80);
        m_pCpufreqLab[i]->setDigitCount(8);
        m_pCpufreqLab[i]->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
        pMainLayout->addWidget(m_pCpufreqLab[i], i, 0, 1, 2);
        pMainLayout->setAlignment(m_pCpufreqLab[i], Qt::AlignCenter);
    }

    m_pTimeLab->setFixedWidth(300);
    m_pTimeLab->setFixedHeight(80);
    m_pTimeLab->setDigitCount(8);
    m_pTimeLab->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
    pMainLayout->addWidget(m_pTimeLab, 0, 3, 1, 2);
    pMainLayout->setAlignment(m_pTimeLab, Qt::AlignCenter);

    m_pTempLab->setFixedWidth(300);
    m_pTempLab->setFixedHeight(80);
    m_pTempLab->setDigitCount(8);
    m_pTempLab->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
    pMainLayout->addWidget(m_pTempLab, 2, 3, 1, 2);
    pMainLayout->setAlignment(m_pTempLab, Qt::AlignCenter);

    this->setJLayout(pMainLayout);
    this->setJTitle(tr("温度曲线"));

    m_TimeCount = QTime::currentTime();
    m_TimeCount.start();

    m_pTimer = new QTimer;
    connect(m_pTimer, SIGNAL(timeout()), this, SLOT(refreshTimeLCD()));
    m_pTimer->setInterval(1000);
    m_pTimer->start();
}

CpuTemp::~CpuTemp()
{
    m_pTimer->stop();
}

void CpuTemp::refreshTimeLCD()
{
    QFile fileCpu0On("/sys/bus/cpu/devices/cpu0/online");
    QFile fileCpu1On("/sys/bus/cpu/devices/cpu1/online");
    QFile fileCpu2On("/sys/bus/cpu/devices/cpu2/online");
    QFile fileCpu3On("/sys/bus/cpu/devices/cpu3/online");

    QFile fileCpu0Freq("/sys/bus/cpu/devices/cpu0/cpufreq/scaling_cur_freq");
    QFile fileCpu1Freq("/sys/bus/cpu/devices/cpu1/cpufreq/scaling_cur_freq");
    QFile fileCpu2Freq("/sys/bus/cpu/devices/cpu2/cpufreq/scaling_cur_freq");
    QFile fileCpu3Freq("/sys/bus/cpu/devices/cpu3/cpufreq/scaling_cur_freq");

    QFile fileTempValue("/sys/class/hwmon/hwmon0/device/temp_value");

    int timecount = m_TimeCount.elapsed() / 1000;
    QTime currentTime;
    currentTime.setHMS((timecount/(60*60))%24, (timecount%(60*60))/60,
                       (timecount%60));

    QString timeStr = currentTime.toString("hh:mm:ss");
    m_pTimeLab->display(timeStr);

    QByteArray cpuMsg;

    fileCpu0On.open(QFile::ReadOnly);
    cpuMsg = fileCpu0On.readAll();
    fileCpu0On.close();
    if(cpuMsg[0] == '1') {
        fileCpu0Freq.open(QFile::ReadOnly);
        m_pCpufreqLab[0]->display(QString(fileCpu0Freq.readAll()));
        fileCpu0Freq.close();
    } else {
        m_pCpufreqLab[0]->display(QString("OFF"));
    }

    fileCpu1On.open(QFile::ReadOnly);
    cpuMsg = fileCpu1On.readAll();
    fileCpu1On.close();
    if(cpuMsg[0] == '1') {
        fileCpu1Freq.open(QFile::ReadOnly);
        m_pCpufreqLab[1]->display(QString(fileCpu1Freq.readAll()));
        fileCpu1Freq.close();
    } else {
        m_pCpufreqLab[1]->display(QString("OFF"));
    }

    fileCpu2On.open(QFile::ReadOnly);
    cpuMsg = fileCpu2On.readAll();
    fileCpu2On.close();
    if(cpuMsg[0] == '1') {
        fileCpu2Freq.open(QFile::ReadOnly);
        m_pCpufreqLab[2]->display(QString(fileCpu2Freq.readAll()));
        fileCpu2Freq.close();
    } else {
        m_pCpufreqLab[2]->display(QString("OFF"));
    }

    fileCpu3On.open(QFile::ReadOnly);
    cpuMsg = fileCpu3On.readAll();
    fileCpu3On.close();
    if(cpuMsg[0] == '1') {
        fileCpu3Freq.open(QFile::ReadOnly);
        m_pCpufreqLab[3]->display(QString(fileCpu3Freq.readAll()));
        fileCpu3Freq.close();
    } else {
        m_pCpufreqLab[3]->display(QString("OFF"));
    }

    fileTempValue.open(QFile::ReadOnly);
    m_pTempLab->display(QString(fileTempValue.readAll()));
    fileTempValue.close();
}
